﻿using System;
using System.CodeDom;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BookStore
{
    public partial class FrmReset : Form
    {
        cTcp tcpClient;
        public FrmReset(cTcp t)
        {
            tcpClient = t;
            InitializeComponent();
        }

        private void LExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Breset_Click(object sender, EventArgs e)
        {
            TResetUserName.Text = "";
            TResetPassword.Text = "";
            TResetIDCard.Text = "";
        }

        private void Bsubmit_Click(object sender, EventArgs e)
        {
            TResetUserName.Text = TResetUserName.Text.Trim();
            TResetPassword.Text = TResetPassword.Text.Trim();
            TResetIDCard.Text = TResetIDCard.Text.Trim();
            //检查输入内容
            if(Log.CheckUserNameAndPassWordAndID(TResetUserName.Text,TResetPassword.Text,TResetIDCard.Text) == false)
            {
                return;
            }
            var msgPack = new data("reset", string.Format("{0},{1},{2}", TResetUserName.Text, TResetPassword.Text, TResetIDCard.Text));
            tcpClient.WriteData(msgPack);
        }

        private void FrmReset_Load(object sender, EventArgs e)
        {
            tcpClient.RestStatusRecieved += ReceiveResetResult;
        }

        private void ReceiveResetResult(string content)
        {
            var d = new data(content);
            string cmd = d.Command;
            string cnt = d.Content;
            if (cmd == "result")
            {

                //重设密码成功
                if (cnt == "RESET_OK")
                {
                    Invoke(new Action(() =>
                    {
                        MessageBox.Show("Success");
                        this.Close();
                    }
                                     ));   
                }
                else if (cnt == "NONE")  // 用户或者ID不存在
                {
                    MessageBox.Show("Username or ID not exist");
                }
            }
            else
            {
                MessageBox.Show("Wrong Reset status code");
            }
        }
    }
}
